/**
 * 描述: 业务逻辑处理 - 任务相关接口
 * 作者: 
 * 日期: 
 */

const { querySql, queryOne } = require('../utils/index');
const jwt = require('jsonwebtoken');
const boom = require('boom');
const {
    CODE_ERROR,
    CODE_SUCCESS,
    PRIVATE_KEY,
    JWT_EXPIRED,
    COOKIE_TOKEN_EXPIRED
} = require('../utils/constant');
const { VueUrl } = require('../config/index');
const { decode } = require('../utils/user-jwt');
const receiptService = require('../sequelize/control/receipt')
const couponService = require('../sequelize/control/couponService')
const driverService = require('../sequelize/control/driverServer')
const couponMgrService = require('../sequelize/control/couponMgrService')
const sendMessageModule = require('../utils/wx_send_template/send_message')

// 查询当前任务列表
async function getCurrentReceipt(req, res) {
    // console.log(req.headers.authorization) //req.headers.authorization
    let truckLic = req.body.truckLic
    const result = await receiptService.currentReceipt(truckLic);
    console.log(result)
    res.send(result);

}
//获取历史小票信息
async function getHistoryReceipt(req, res) {
    let truckLic = req.body.truckLic;
    let pageNo = req.body.pageNo - 1;
    let pageSize = req.body.pageSize
    const result = await receiptService.historyReceipt(truckLic, pageNo, pageSize);
    res.send(result);
}
//大桥证优惠
async function getCouponList(req, res) {
    console.log(req.body)
    var truckLic = req.body.TruckLic;
    var direction = req.body.Direction;
    var result = await couponService.couponList(truckLic, direction);
    console.log(result)
    res.send(result);

}
//发送新小票模版消息
async function sendMessage(req, res) {
    var sendMessage = req.body;
    sendMessage = JSON.parse(JSON.stringify(sendMessage));
    var userInfo = await driverService.getDriverId(sendMessage.TruckLic);
    sendMessage = {...userInfo, ...sendMessage };
    var result = await sendMessageModule.SendMsgToDriver(sendMessage);
    console.log(result)
    res.json({ code: 1, msg: 'ok' });
}

//发送服务模版消息
async function sendMsgToAll(req, res) {
    var sendMessage = req.body;
    sendMessage = JSON.parse(JSON.stringify(sendMessage));
    await sendMessageModule.SendMsgToAll(sendMessage);
    res.json({ code: 1, msg: 'ok' });
}

//使用大桥正优惠并记录使用记录
async function useCoupon(req, res) {
    let limit_flag = req.body.Direction == '0' ? 'in' : 'out';
    let rawValue = await couponMgrService.updateCouponMgr(limit_flag);
    if (rawValue.code == 1) {
        await receiptService.modifyReceipt(req.body);
        await couponService.useCoupon(req.body);
        res.send({ code: 1 });
    } else {
        res.send({ code: 0 });
    }
}
/**
 * {
  StatusNow: 4,
  ReceiptID: 2,
  CouponID: 2,
  Direction: '0',
  RedeemTime: '2022-02-21 16:03:56',
  TruckLic
}
 */

//堆场信息
function gardenInfo(req, res) {
    let place = req.query.ContainerPlace;
    if (place != undefined) {
        place = place.slice(0, 4);
        console.log(typeof(place))
    } else {
        place = '0000';
    }
    res.render('gardenInfo', { ContainerPlace: place, url: VueUrl });

}


//修改小票状态
function toComplishBtn(req, res) {
    let ReceiptId = req.query.ReceiptId;
    //小票完成按钮
    receiptService.toComplish(ReceiptId);
    res.send({ code: 1 });
}
module.exports = {
    getCurrentReceipt,
    getHistoryReceipt,
    getCouponList,
    sendMessage,
    useCoupon,
    sendMsgToAll,
    gardenInfo,
    toComplishBtn
}